/*项目依赖*/
const Koa = require("koa");
const koaJson = require("koa-json"); // get提交数据的中间件
const bodyParser = require("koa-bodyparser"); // post提交数据中间件
const koaBody = require("koa-body"); // 文件上传
const path = require("path");
const http = require("http");
const routes = require("./routes");
const staticFiles = require("koa-static");
const Mount = require("koa-mount");
/*工具方法*/
// const util = require('./utilitys.js');
/*配置文件*/
const config = require("./config.js");
/*配置属性*/
const { SERVICE } = config;
/*应用实例*/
const app = new Koa();

app.use(bodyParser());
app.use(koaJson());
app.use(
	koaBody({
		multipart: true,
		formidable: {
			maxFileSize: config.LIMIT.UPLOAD_IMG_SIZE, // 设置上传文件大小最大限制，默认2M
		},
	})
);
// app.use(async (ctx, next) => {
//   ctx.execSql = util.query;
//   await next();
// });
const { Sequelize } = require("sequelize");
app.use(async (ctx, next) => {
	// 方法 2: 分别传递参数 (sqlite)
	const sequelize = new Sequelize({
		dialect: "sqlite",
		// storage: path.join(__dirname, "../db/db.sqlite3"),
		storage: path.join(__dirname, "../db/test.db"),
	});
	ctx.sequelize = sequelize;
	await next();
});

// console.log('dist', path.join(__dirname, '../dist/'));
// app.use(Static('./web'),{defer:true})
const { historyApiFallback } = require('koa2-connect-history-api-fallback')
app.use(

	historyApiFallback({
  
	  enable: true
  
	})
  
  )
app.use(staticFiles(path.join(__dirname, "../dist/")), { defer: true });
app.use(Mount("/svg", staticFiles(path.join(__dirname, "../svg/"))));
app.use(Mount("/design", staticFiles(path.join(__dirname, "../design/"))));

/*路由配置*/
app.use(routes.routes());
/*web服务*/
http.createServer(app.callback())
	.listen(SERVICE.PORT)
	.on("listening", function() {
		console.log(`服务已开启，端口：http://localhost:${SERVICE.PORT}`);
	});

const watcher = require("./service/watchDesignFiles")
